<template>
  <el-card>
    <h3>医馆管理</h3>

    <!-- 筛选与搜索 -->
    <el-form :inline="true" @submit.prevent="fetchClinics">
      <el-form-item label="状态">
        <el-select v-model="filters.status" placeholder="请选择状态">
          <el-option label="全部" value=""></el-option>
          <el-option label="待审核" value="pending"></el-option>
          <el-option label="已通过" value="approved"></el-option>
          <el-option label="已拒绝" value="rejected"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="关键词">
        <el-input v-model="filters.keyword" placeholder="医馆名/地址"></el-input>
      </el-form-item>
      <el-button type="primary" @click="fetchClinics">搜索</el-button>
    </el-form>

    <!-- 医馆列表 -->
    <el-table :data="clinics" border style="width: 100%">
      <el-table-column prop="clinicName" label="医馆名称"></el-table-column>
      <el-table-column prop="doctorName" label="大夫名称"></el-table-column>
      <el-table-column label="地址">
        <template #default="scope">
          {{ scope.row.province }} {{ scope.row.city }} {{ scope.row.district }} {{ scope.row.detailedAddress }}
        </template>
      </el-table-column>
      <el-table-column prop="specialty" label="擅长主治"></el-table-column>
      <el-table-column label="状态">
        <template #default="scope">
          <el-tag v-if="scope.row.status === 'pending'" type="warning">待审核</el-tag>
          <el-tag v-else-if="scope.row.status === 'approved'" type="success">已通过</el-tag>
          <el-tag v-else type="danger">已拒绝</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template #default="scope">
          <el-button size="small" @click="viewDetail(scope.row)">查看详情</el-button>
          <el-button size="small" @click="approveClinic(scope.row.id, 'approved')">通过</el-button>
          <el-button size="small" type="danger" @click="approveClinic(scope.row.id, 'rejected')">拒绝</el-button>
          <el-button size="small" type="danger" @click="deleteClinic(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-pagination
        layout="prev, pager, next"
        :total="total"
        :page-size="filters.pageSize"
        @current-change="handlePageChange"
    />
  </el-card>

  <!-- 详情弹窗 -->
  <el-dialog title="医馆详情" v-model="detailDialogVisible">
    <p><strong>医馆名称：</strong>{{ selectedClinic.clinicName }}</p>
    <p><strong>大夫名称：</strong>{{ selectedClinic.doctorName }}</p>
    <p><strong>地址：</strong>{{ selectedClinic.province }} {{ selectedClinic.city }} {{ selectedClinic.district }} {{ selectedClinic.detailedAddress }}</p>
    <p><strong>擅长主治：</strong>{{ selectedClinic.specialty }}</p>
    <p><strong>备注：</strong>{{ selectedClinic.remarks }}</p>
    <img :src="selectedClinic.licenseImageUrl" alt="执照" v-if="selectedClinic.licenseImageUrl" style="max-width: 100%">
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      clinics: [],
      total: 0,
      filters: {
        page: 1,
        pageSize: 10,
        status: '',
        keyword: ''
      },
      detailDialogVisible: false,
      selectedClinic: {}
    }
  },
  methods: {
    async fetchClinics() {
      const res = await this.$axios.get('/api/admin/clinics', { params: this.filters });
      this.clinics = res.data.data;
      this.total = res.data.total;
    },
    handlePageChange(page) {
      this.filters.page = page;
      this.fetchClinics();
    },
    viewDetail(clinic) {
      this.selectedClinic = clinic;
      this.detailDialogVisible = true;
    },
    async approveClinic(id, status) {
      await this.$axios.put(`/api/admin/clinics/${id}/approve`, { status });
      this.$message.success('操作成功');
       this.fetchClinics();
    },
    async deleteClinic(id) {
      await this.$axios.delete(`/api/admin/clinics/${id}`);
      this.$message.success('删除成功');
      this.fetchClinics();
    }
  },
  mounted() {
    this.fetchClinics();
  }
}
</script>